// ==UserScript==
// @name          Ikariam - Make Map
// @version       1.0
// @date          2008-09-01
// @namespace     archeorbiter.googlecode.com
// @description   Version 1.0 - Rewrites the "Show World" link to work with Johan Sundström's interactive map on ikariam.ogame-world's town search result pages.
// @include       http://ikariam.ogame-world.*/suche.php?view=suche_stadt&land_i=*
// ==/UserScript==

// !For now, it only works on the first page of the results!

// For the underlying technology full credit to Johan Sundström.
// Release thread and further explanations: http://corentin.jarnoux.free.fr/kronosutils/index.php?topic=93.0

// --- Helper Functions --- //

// Credit to Johan Sundström
function $x( xpath, root ) {
	var doc = root ? root.evaluate ? root : root.ownerDocument : document, next;
	var got = doc.evaluate( xpath, root||doc, null, 0, null ), result = [];
	switch (got.resultType) {
		case got.STRING_TYPE:
			return got.stringValue;
		case got.NUMBER_TYPE:
			return got.numberValue;
		case got.BOOLEAN_TYPE:
			return got.booleanValue;
		default:
			while (next = got.iterateNext())
				result.push( next );
				return result;
	}
}
function $X( xpath, root ) {
	var got = $x( xpath, root );
	return got instanceof Array ? got[0] : got;
}

function urlParse(param, url) {
	if (!url) url = location.search || "";
	var keys = {};
	url.replace(/([^=&?]+)=([^&#]*)/g, function(m, key, value) {
		keys[decodeURIComponent(key)] = decodeURIComponent(value);
	});
	return (param ? keys[param] : keys) || "view" == param && document.body.id;
}

// Credit to Martin Withaar
function unique(a) {
	var r = new Array();
	o:for(var i = 0, n = a.length; i < n; i++) {
		for(var x = 0, y = r.length; x < y; x++)
			if(r[x]==a[i]) continue o;
		r[r.length] = a[i];
	}
	return r;
}
// --- /Helper Functions --- //

// ------- Settings -------- //
var type = '0'; // 0: show nothing, 1: show resource types, 2: show +10% wonders, 3: show both
var center = '1'; // island id on which the map should be initially centered
var width = '50'; // initial width (max 100)
var height = '50'; // initial height (max 100)
var color = 'purple'; // highlight color (accepts every type of HTML compatible color code)
var opacity = '100'; // color opacity (0 (invisible to 100 (fully visible))) 
// ------- /Settings ------- //

var cities = $x('id("main")//table[2]/tbody/tr/td[@class="body"][4]/a');
var domain = $X('id("main")//table[1]/tbody/tr[1]/td[2]/select/option[@selected="selected"]');
var servNumb = $X('id("main")//table[1]/tbody/tr[2]/td[2]/select/option[@selected="selected"]');
var server = 's'+ servNumb.value +'.'+ domain.textContent;

function makeMap() {
	function makeArray(elem) {
		var islandHref = elem.href;
		var islandId = urlParse("id", islandHref);
		islandIds.push(islandId);
	}

	var islandIds = [];
	cities.forEach(makeArray);
	islandIds = unique(islandIds);

	var mapUrl = 'http://ecmanaut.googlecode.com/svn/trunk/sites/ikariam.org/kronos-utils/minimap.html?s='+ 
                server +'&r='+ type +'&c='+ center +'&w='+ width +'&h='+ height +'&'+ color +'/'+ opacity +'='+ islandIds;

	window.open(mapUrl);
}

var mapLink = $X('id("main")//table[2]/tbody/tr[1]/td[1]/a');
mapLink.setAttribute('href', '#');
mapLink.setAttribute('target', '');
mapLink.addEventListener('click', makeMap, false);
